home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 644 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  55 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: JamesCurran@CIS.CompuServe.Com (James M. Curran)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: ugly constants and header files
  5. Date: Mon, 08 Jan 1996 04:26:13 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4cq6d8$t51@dub-news-svc-1.compuserve.com>
  8. References: <1996Jan3.155754.111142@kuhub.cc.ukans.edu>
  9. NNTP-Posting-Host: ad08-014.compuserve.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. In <1996Jan3.155754.111142@kuhub.cc.ukans.edu>, anh@kuhub.cc.ukans.edu
  13. wrote:
  14.  
  15.  >>Is this the best solution? Are there any other way?
  16.  
  17.     To expand a bit on David Wright's method, in the common header file,
  18. use:
  19.  
  20.     #if defined(GLOBAL)
  21.         #define  PUBLIC
  22.         #define  VALUE(n)      = n
  23.     #else
  24.         #define  PUBLIC    extern
  25.         #define  VALUE
  26.     #endif
  27.  
  28.  
  29.   then declare your variable in the following form:
  30.  
  31.     PUBLIC int nCount VALUE(5);
  32.     PUBLIC char *sName VALUE("Hello, World!");
  33.  
  34.   then, again, #define GLOBAL at the top of only one module.  When
  35. this module  is compiled, the code will be rendered as:
  36.  
  37.         int nCount = 5;
  38.         char *sName = "Hello, World!"l
  39.  
  40.   However, for the other file, it will be translated as:
  41.  
  42.     extern int nCount;
  43.     extern char *sName;
  44.  
  45.   NOTE -- This method works for works fine for scalars, but chokes on
  46. array declarions.  You'll have to fall back to David's method from
  47. them.
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.